#include <gtest/gtest.h>
#include "common.h"

using namespace ::std;

class Solution {
public:
    bool hasCycle(ListNode *head);
};

bool Solution::hasCycle(ListNode *head) {
    if(head == nullptr) return false;
    ListNode *slowPoint, *fastPoint;
    slowPoint = fastPoint = head;
    while(fastPoint != nullptr) {
        slowPoint = slowPoint->next;
        fastPoint = fastPoint->next != nullptr ? fastPoint->next->next : nullptr;
        if(fastPoint == slowPoint && slowPoint != nullptr) return true;
    }
    return false;
}

class HasCycleCheck : public ::testing::Test {
public:
	class Solution solution;
};

TEST_F(HasCycleCheck, testHasCycle_1) {
    int pos = 1;
    vector<int> value = {3,2,0,-4};
    ListNode* head = makeCycleLinkedList(value, pos);
    EXPECT_TRUE(true == solution.hasCycle(head));
}

TEST_F(HasCycleCheck, testHasCycle_2) {
    int pos = 0;
    vector<int> value = {1,2};
    ListNode* head = makeCycleLinkedList(value, pos);
    EXPECT_TRUE(true == solution.hasCycle(head));
}

TEST_F(HasCycleCheck, testHasCycle_3) {
    int pos = -1;
    vector<int> value = {1};
    ListNode* head = makeCycleLinkedList(value, pos);
    EXPECT_TRUE(false == solution.hasCycle(head));
}

int main() {
	::testing::InitGoogleTest();
	return RUN_ALL_TESTS();
}
